import React from 'react'
import { Form, Input, Button, NavBar } from 'antd-mobile'
import './Login.scss'

export default class Login extends React.Component {

    
    state = {
        name: '',
        parssward: '',
    }

    // d登陆
    onLogin = async () => {
        const { name, parssward } = this.state;
        
        let parma ={
            "email": name,
            "pass": parssward
          }

        const res = await this.$get_request(this, parma, '用户登陆', 'my', 'login')
        if (!res) {
            debugger
            // this.register() 
            return false
        }
        localStorage.setItem('boke_city', res.token)
    }

    // 注册
    register = async () => {
        const { name, parssward } = this.state;
        
        let parma ={
            "avatar": "https://avatars.githubusercontent.com/u/33335359?s=60&v=4",
            "email": name,
            "name": name,
            "pass": parssward
          }
        const res = await this.$get_request(this, parma, '用户注册', 'my', 'user')
        if (!res) {
            debugger
            // this.register() 
            return false
        }
    }

    back = () => {
        this.props.history.go(-1)
    }

    render() {
        return (
            <div className="Login">
                <NavBar back='返回' onBack={this.back}>登 陆</NavBar>
                <div className="shangbaiao">
                    <img src="https://avatars.githubusercontent.com/u/33335359?s=60&v=4" alt="" />
                </div>
                <div className="login-from">
                <Form layout='horizontal'
                    footer={
                        <Button onClick={this.onLogin} block type='submit' color='primary'>
                            登 陆
                        </Button>
                    }
                >
                    <Form.Item
                        name='name'
                        label='账号'
                        rules={[{ required: true, message: '账号不能为空' }]}
                    >
                        <Input onChange={(e) => { this.setState({ name: e }) }} placeholder='请输入账号' />
                    </Form.Item>
                    <Form.Item name='parssward' label='密码'
                        rules={[{ required: true, message: '密码不能为空' }]}>
                        <Input onChange={(e) => { this.setState({ parssward: e }) }} placeholder='请输入密码' />
                    </Form.Item>
                </Form>

                </div>
                <div className="footTEXT">
                    <span>未注册的用户将自动为您注册！</span>
                </div>
                
            </div>
        )
    }
}